package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSONArray;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.model.commond.WebInspectionitemValue;
import com.sidianzhong.sdz.model.commond.Webequipmentitemgroup;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.ResultModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "设备巡机数据项目表")
@Controller
public class WebInspectionitemController {

    @Autowired
    WebInspectionitemService webInspectionitemService;
    @Autowired
    WebInspectiondatarecordService webInspectiondatarecordService;
    @Autowired
    WebHequipmentitemgroupService webHequipmentitemgroupService;
    @Autowired
    WebZequipmentitemgroupService webZequipmentitemgroupService;
    @Autowired
    AppHequipmentgrouprecordService appHequipmentgrouprecordService;
    @Autowired
    AppZequipmentgrouprecordService appZequipmentgrouprecordService;
    @Autowired
    HttpServletResponse response;


    @ApiOperation(value = "扫码获取设备")
    @RequestMapping(value = "/web_inspectionitems",
            method = { RequestMethod.GET },
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getWebInspectionitems(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder,
            @RequestParam(value = "equipmenttypekey", required = false) String equipmenttypekey,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey
    ) {
        WebInspectionitem item = new WebInspectionitem();
        if( equipmenttypekey != null ){
            item.setEquipmenttypekey(equipmenttypekey);
        }
        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }

        List<WebInspectionitem> list = webInspectionitemService.getList(pageNum, pageSize, sortItem, sortOrder, item);

        List<WebInspectionitemValue> collect = list.stream().map(lt -> {
            WebInspectionitemValue webInspectionitemValue = new WebInspectionitemValue();
            webInspectionitemValue.setId(lt.getId());
            webInspectionitemValue.setEquipmenttypekey(lt.getEquipmenttypekey());
            webInspectionitemValue.setEquipmentkey(lt.getEquipmentkey());
            webInspectionitemValue.setItemname(lt.getItemname());
            webInspectionitemValue.setItemtype(lt.getItemtype());
            webInspectionitemValue.setItemunits(lt.getItemunits());
            webInspectionitemValue.setItemoption1(lt.getItemoption1());
            webInspectionitemValue.setItemoption2(lt.getItemoption2());
            webInspectionitemValue.setItemoption3(lt.getItemoption3());
            webInspectionitemValue.setItemoption4(lt.getItemoption4());

            WebInspectiondatarecord bykey = webInspectiondatarecordService.getBykey(lt.getEquipmenttypekey(), lt.getEquipmentkey(),lt.getId());
            webInspectionitemValue.setItemvalue(null==bykey ? String.valueOf(0):bykey.getItemvalue());
            return webInspectionitemValue;
        }).collect(Collectors.toList());
        return new ResponseEntity<>(ResultModel.ok(collect), HttpStatus.OK);

    }


    @ApiOperation(value = "查询扫码获取设备")
    @RequestMapping(value = "/inspectionitems",
            method = { RequestMethod.GET },
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object inspectionitems(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder,
            @RequestParam(value = "recordId", required = false) Integer recordid,
            @RequestParam(value = "equipmenttypekey", required = false) String equipmenttypekey,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey
    ) {
        WebInspectionitem item = new WebInspectionitem();
        if( equipmenttypekey != null ){
            item.setEquipmenttypekey(equipmenttypekey);
        }
        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }

        List<WebInspectionitem> list = webInspectionitemService.getList(pageNum, pageSize, sortItem, sortOrder, item);

        List<WebInspectionitemValue> collect = list.stream().map(lt -> {
            WebInspectionitemValue webInspectionitemValue = new WebInspectionitemValue();
            webInspectionitemValue.setId(lt.getId());
            webInspectionitemValue.setEquipmenttypekey(lt.getEquipmenttypekey());
            webInspectionitemValue.setEquipmentkey(lt.getEquipmentkey());
            webInspectionitemValue.setItemname(lt.getItemname());
            webInspectionitemValue.setItemtype(lt.getItemtype());
            webInspectionitemValue.setItemunits(lt.getItemunits());
            webInspectionitemValue.setItemoption1(lt.getItemoption1());
            webInspectionitemValue.setItemoption2(lt.getItemoption2());
            webInspectionitemValue.setItemoption3(lt.getItemoption3());
            webInspectionitemValue.setItemoption4(lt.getItemoption4());

            WebInspectiondatarecord bykey = webInspectiondatarecordService.getOneBykey(recordid,lt.getEquipmenttypekey(), lt.getEquipmentkey(),lt.getId());
            webInspectionitemValue.setItemvalue(null==bykey ? String.valueOf(0):bykey.getItemvalue());
            return webInspectionitemValue;

        }).collect(Collectors.toList());
        return new ResponseEntity<>(ResultModel.ok(collect), HttpStatus.OK);

    }

    @ApiOperation(value = "巡查扫码获取设备")
    @RequestMapping(value = "/xc_inspectionitems",
            method = { RequestMethod.GET },
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getxcWebInspectionitems(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder,
            @RequestParam(value = "equipmenttypekey", required = false) String equipmenttypekey,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey
    ) {
        List<Webequipmentitemgroup> collect = new ArrayList<>();
        if (equipmenttypekey.equals("8")){
            WebHequipmentitemgroup item = new WebHequipmentitemgroup();
            item.setEquipmentkey(equipmentkey);
            List<WebHequipmentitemgroup> list = webHequipmentitemgroupService.getList(pageNum, pageSize, sortItem, sortOrder, item);
            collect = list.stream().map(lt -> {
                AppHequipmentgrouprecord one = appHequipmentgrouprecordService.getOne(equipmentkey, lt.getOwnkey());
                Webequipmentitemgroup webequipmentitemgroup = new Webequipmentitemgroup();
                webequipmentitemgroup.setEquipmentkey(lt.getEquipmentkey());
                webequipmentitemgroup.setOwnkey(lt.getOwnkey());
                webequipmentitemgroup.setRoomname(lt.getOwnname());
                webequipmentitemgroup.setValue(one.getValue());
                webequipmentitemgroup.setValuesize(lt.getValuesize());
                webequipmentitemgroup.setValuetype(one.getValuetype());
                return webequipmentitemgroup;

            }).collect(Collectors.toList());


        }
        if (equipmenttypekey.equals("30")){
            WebZequipmentitemgroup item = new WebZequipmentitemgroup();
            item.setEquipmentkey(equipmentkey);
            List<WebZequipmentitemgroup> list = webZequipmentitemgroupService.getList(pageNum, pageSize, sortItem, sortOrder, item);
            collect = list.stream().map(lt -> {
                AppZequipmentgrouprecord one = appZequipmentgrouprecordService.getOne(equipmentkey, lt.getOwnkey());
                Webequipmentitemgroup webequipmentitemgroup = new Webequipmentitemgroup();
                webequipmentitemgroup.setEquipmentkey(lt.getEquipmentkey());
                webequipmentitemgroup.setOwnkey(lt.getOwnkey());
                webequipmentitemgroup.setRoomname(lt.getOwnname());
                webequipmentitemgroup.setValue(one.getValue());
                webequipmentitemgroup.setValuesize(lt.getValuesize());
                webequipmentitemgroup.setValuetype(one.getValuetype());
                return webequipmentitemgroup;

            }).collect(Collectors.toList());
        }

        return new ResponseEntity<>(ResultModel.ok(collect), HttpStatus.OK);

    }
}
